import type { PlasmoCSConfig } from "plasmo"

export const config: PlasmoCSConfig = {
  matches: ["https://pytdc.pyedu.cn/*"],
  world: "MAIN",
  run_at: "document_start"
}

// window.addEventListener("load", () => {
//   setTimeout(() => {
//     let videoElement = document.createElement('video');
//     videoElement.style.width = '100%';
//     videoElement.style.height = '100%';
//     videoElement.controls = true;
//     videoElement.src = 'https://pytdc.pyedu.cn/alink/group1/M00/02/0E/rBMWIWd2MZCEWDvLAAAAAA1EYaU650.mp4?token=3bee4ab560ac65351476b52cbbe24525&ts=1747824579'
//     document.getElementsByClassName('video-player')[0].appendChild(videoElement);
//   }, 1000)
// })

var XHR = XMLHttpRequest.prototype;
var open = XHR.open;
// var send = XHR.send;

XHR.open = function (method, url) {
  this._method = method;
  this._url = url;
  // console.log('xhr request:', this._method, this._url);
  this.addEventListener('load', function () {
    try {
      let json = JSON.parse(this.response);
      if (json.hasOwnProperty('data') && json.data.hasOwnProperty('fileUri')) {
        // 找到视频的地址
        // 先把需要密码的弹框去掉
        let passwordElement = document.getElementsByClassName('password-view')[0];
        if (passwordElement) {
          passwordElement.remove();
        }
        // 把正确的视频地址替换到页面中
        let videoElement = document.createElement('video');
        videoElement.style.width = '100%';
        videoElement.style.height = '100%';
        videoElement.controls = true;
        videoElement.src = 'https://pytdc.pyedu.cn/alink/' + json.data.fileUri;
        let videoArea = document.getElementsByClassName('video-player')[0];
        if (videoArea.firstChild) {
          videoArea.insertBefore(videoElement, videoArea.firstChild);
        } else {
          videoArea.appendChild(videoElement);
        }
      }
    } catch (e) {
      console.log(e);
    }
  });
  return open.apply(this, arguments);
}
// XHR.send = function (postData) {
//     return send.apply(this, arguments);
// };